package com.zhijian.warehouse.manage.controller.app;

import com.zhijian.response.annotation.Response;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.entity.dto.UserDto;
import com.zhijian.warehouse.manage.entity.vo.request.StoreStockRecordSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreStockRecordSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreStockRecordVo;
import com.zhijian.warehouse.manage.enums.StockActionEnum;
import com.zhijian.warehouse.manage.enums.StoreEnum;
import com.zhijian.warehouse.manage.holder.CurrentMpUserHolder;
import com.zhijian.warehouse.manage.service.StoreStockRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 盘点记录 控制层
 *
 * @author auto
 * @version 1.0
 * @date 2024-10-27 13:46:26
 */
@Slf4j
@Controller
@Api(tags = "app库存盘点")
@RequestMapping("/app")
public class AppStoreStockRecordController {

    @Autowired
    private StoreStockRecordService service;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/store/stock/record")
    @ApiOperation("新增")
    public boolean insert(@RequestBody StoreStockRecordSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        UserDto user = CurrentMpUserHolder.getUser();
        param.setCreateUserId(user.getId());
        param.setUpdateUserId(user.getId());
        return service.save(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/store/stock/record/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/store/stock/record/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody StoreStockRecordSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        UserDto user = CurrentMpUserHolder.getUser();
        param.setUpdateUserId(user.getId());
        return service.modify(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/store/stock/record/{id}")
    @ApiOperation("根据id查询")
    public StoreStockRecordVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/store/stock/record/page")
    @ApiOperation("分页查询")
    public Page<StoreStockRecordVo> selectByCondition(@RequestBody(required = false) StoreStockRecordSearchVo param) {
        if (param == null)
            param = StoreStockRecordSearchVo.builder().build();
        log.info("{} - {}| selectByCondition info", this.getClass().getSimpleName(), param);
        param.setType(StoreEnum.STOCK_TYPE.BALANCE.getCode());//列表仅查询原始单据
        return service.selectPage(param);
    }

    /**
     * 列表查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/store/stock/record/list")
    @ApiOperation("列表查询")
    public List<StoreStockRecordVo> selectList(@RequestBody(required = false) StoreStockRecordSearchVo param) {
        if (param == null)
            param = StoreStockRecordSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectList(param);
    }

    /**
     * 提交盘点
     *
     * @param param
     * @return
     */
    @Response
    @PutMapping("/store/submit/stock")
    @ApiOperation("提交盘点")
    public boolean submitStock( @RequestBody(required = false) StoreStockRecordSaveOrUpdateVo param) {
        log.info("{} - {}| submitStock info, {}", this.getClass().getSimpleName(), param);
        UserDto user = CurrentMpUserHolder.getUser();
        param.setUpdateUserId(user.getId());
        return service.submitStock(param);
    }

    /**
     * 提交盘点审核
     *
     * @param param
     * @return
     */
    @Response
    @PutMapping("/store/stock/submit")
    @ApiOperation("提交盘点审核")
    public boolean submitAuditStock( @RequestBody(required = false) StoreStockRecordSaveOrUpdateVo param) {
        log.info("{} | submitAuditStock info, {}", this.getClass().getSimpleName(), param);
        if (param == null)
            param = StoreStockRecordSaveOrUpdateVo.builder().build();
        UserDto user = CurrentMpUserHolder.getUser();
        param.setUpdateUserId(user.getId());
        param.setStockUserId(user.getId());
        return service.auditStock(param);
    }

    /**
     * 根据根据盈亏单号查询盘点记录
     *
     * @param number
     * @return
     */
    @Response
    @GetMapping("/store/stock/record/monad/{number}")
    @ApiOperation("根据根据盈亏单号查询盘点记录")
    public StoreStockRecordVo selectMonadById(@PathVariable("number") String number) {
        log.info("{} - {}| selectMonadById info", this.getClass().getSimpleName(), number);
        return service.selectVoByNumber(number);
    }

    /**
     * 盘盈亏审核
     *
     * @param id
     * @param saveOrUpdateVo
     * @return
     */
    @Response
    @PutMapping("/store/stock/audit/{id}")
    @ApiOperation("盘盈亏审核")
    public boolean monadAuditStock(@PathVariable("id") Long id, @RequestBody StoreStockRecordSaveOrUpdateVo saveOrUpdateVo) {
        log.info("{} - {}| monadAuditStock info, {}", this.getClass().getSimpleName(), id, saveOrUpdateVo);
        if (saveOrUpdateVo == null)
            saveOrUpdateVo = StoreStockRecordSaveOrUpdateVo.builder().build();
        //saveOrUpdateVo.setId(id);
        saveOrUpdateVo.setUpdateUserId(CurrentMpUserHolder.getUser().getId());
        saveOrUpdateVo.setAudiUserName(CurrentMpUserHolder.getUser().getName());
        return service.monadAuditStock(saveOrUpdateVo, StockActionEnum.OPT_SOURCE.WEB.value);
    }
    /**
     * 获取最新的单号
     *
     * @return
     */
    @Response
    @GetMapping("/store/stock/next/number")
    @ApiOperation("获取最新的单号")
    public String createNextNumber() {
        return service.createNextNumber();
    }

}
